package Business;

import Message.Tips;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 房间类
 */
public class Room {

    /**
     * 空闲房间列表
     */
    private static final List<String> roomList = new ArrayList<>();

    /**
     * 数字格式
     */
    private static final DecimalFormat numFormat = new DecimalFormat("00000");

    /**
     * 最大房间数量
     */
    public static final int maxRoomSize = 10;

    /**
     * 删除音乐
     */
    public static boolean removeMusic(String room,int id){
        return Mysql.Update.removeRoomMusic(room , id);
    }

    /**
     * 获取音乐列表数量
     */
    public static int getMusicCount(String room){
        return Mysql.Query.getRoomMusicCount(room);
    }

    /**
     * 获取房间音乐播放时间
     */
    public static Date getMusicDate(String room){
        Map<String , Object> base = Mysql.Query.getRoomBase(room);
        if (base == null) return null;
        return (Date) base.get("musicDate");
    }

    /**
     * 获取房间播放音乐列表
     */
    public static List<Object> getMusicList(String room){
        return Mysql.Query.getRoomMusic(room);
    }

    /**
     * 房间添加音乐
     * @param room 房间号
     * @param id 音乐id
     * @return 返回房间音乐id
     */
    public static boolean addMusic(String room, String id){
        Map<String , Object> music = Music.getMusicInfo(id);
        boolean musicErr = music == null || music.size() <= 0;
        if (musicErr) return false;
        String musicId = music.get("id").toString();
        String name = music.get("name").toString();
        String artists = music.get("artists").toString();
        int duration = (int) music.get("duration");
        String imageUrl = music.get("imageUrl").toString();
        String url = music.get("url").toString();
        boolean vip = (boolean) music.get("vip");
        if (Mysql.Query.getRoomMusicCount(room) <= 0){//更新房间音乐播放时间
            if (!Mysql.Update.updRoomMusicDate(room , false)) return false;
        }
        return Mysql.Update.roomAddMusic(room, musicId, name, artists, duration, imageUrl, url , vip);//添加音乐
    }


    /**
     * 转换到Strroom
     */
    public static String toStrRoom(int room){
        return numFormat.format(room);
    }

    /**
     * 获取一个房间
     * @return -1 则异常或房间已满
     */
    public static String getRoom(){
        String room;
        synchronized (roomList){//加锁
            if (roomList.size() == 0){
                return "-1";
            }
            int index = new Random().nextInt(roomList.size());
            room = roomList.get(index);
            if(!Mysql.Update.createRoom(room))//失败
                return "-1";
            roomList.remove(index);
        }
        return room;
    }

    /**
     * 归还房间
     */
    private static void returnRoom(String room){
        synchronized (roomList){//加锁
            if(!Mysql.Update.deleteRoom(room))//失败
                return;
            roomList.add(room);
        }
    }

    /**
     * 用户加入房间
     */
    public static boolean addUser(String openid,String room){
        return Mysql.Update.roomAddUser(openid, room);
    }

    /**
     * 用户退出房间
     */
    public static boolean removeUser(String openid){
        Map<String , Object> userinfo = Mysql.Query.getUser(openid);
        if (userinfo == null) return false;
        int room = (int) userinfo.get("room");
        if (room == -1) return true;
        String roomStr = toStrRoom(room);
        if (!Mysql.Update.roomRemoveUser(openid , roomStr)) return false;
        returnRoom(roomStr);//归还房间
        return true;
    }

    /**
     * 房间初始化
     */
    public static boolean init(){
        Date startDate = new Date();
        synchronized (roomList){//加锁
            if(!Mysql.Update.initRoom()){
                return false;
            }
            for (int i = 0 ; i < maxRoomSize ; i++){
                roomList.add(numFormat.format(i));
            }
        }
        Date endDate = new Date();
        long span = endDate.getTime() - startDate.getTime();
        Tips.Info("本次MYSQL复位连接延时 " + span + " 毫秒");
        return true;
    }





}
